package com.yunhe.abnormal.controller.base;

import com.yunhe.abnormal.domain.base.AbnormalFormat;
import com.yunhe.common.exception.ArgumentErrorException;
import com.yunhe.common.exception.ObjectNotFoundException;
import com.yunhe.common.exception.UniqueConstraintsException;
import com.yunhe.common.model.ResultObject;
import com.yunhe.common.model.base.AbnormalFormatDTO;
import com.yunhe.abnormal.service.base.AbnormalDetailFormatService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@Api(value = "abnormalDetailFormat",description = "异常详情格式", tags = {"Base_AbnormalDetailFormat"})
@RestController
@RequestMapping("/format/abnormalDetail")
public class AbnormalDetailFormatController {

    @Autowired
    private AbnormalDetailFormatService abnormalDetailFormatService;

    @ApiOperation("新增异常详情格式")
    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity<ResultObject> post(@RequestBody AbnormalFormat abnormalFormat)
            throws ArgumentErrorException, UniqueConstraintsException {
        AbnormalFormatDTO newAbnormalFormat = abnormalDetailFormatService.create(abnormalFormat);
        return new ResponseEntity<>(new ResultObject<>(newAbnormalFormat), HttpStatus.OK);
    }

    /**
     * 根据ID删除异常详情格式
     * @param id 异常详情格式ID
     */
    @ApiOperation("根据ID删除异常详情格式")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "异常详情格式ID", required = true, dataType = "long", paramType = "path")
    })
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public ResponseEntity<ResultObject> delete(@PathVariable("id") Long id) {
        abnormalDetailFormatService.delete(id);
        return new ResponseEntity<>(new ResultObject<>(null), HttpStatus.OK);
    }

    /**
     * 更新异常详情格式
     * @param id 异常详情格式ID
     * @param content 内容
     */
    @ApiOperation("更新异常详情格式")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "异常详情格式ID", required = true, dataType = "long", paramType = "path"),
            @ApiImplicitParam(name = "content", value = "异常详情格式内容", dataType = "string", paramType = "query")
    })
    @RequestMapping(value = "/{id}", method = RequestMethod.PATCH)
    public ResponseEntity<ResultObject> patch(@PathVariable("id") Long id,
                                              @RequestParam("code") Optional<Long> code,
                                              @RequestParam("name") Optional<String> name,
                                              @RequestParam("title") Optional<String> title,
                                              @RequestParam("content") Optional<String> content)
            throws ArgumentErrorException, ObjectNotFoundException, UniqueConstraintsException {
        AbnormalFormatDTO newAbnormalFormat = abnormalDetailFormatService.update(id, code, name, title, content);
        return new ResponseEntity<>(new ResultObject<>(newAbnormalFormat), HttpStatus.OK);
    }

    /**
     * 根据异常详情格式ID获得异常详情格式
     * @param id 异常详情格式ID
     */
    @ApiOperation("根据异常详情格式ID获得异常详情格式")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "异常详情格式ID", required = true, dataType = "long", paramType = "path")
    })
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> get(@PathVariable("id") Long id) {
        AbnormalFormatDTO abnormalFormat = abnormalDetailFormatService.findById(id);
        return new ResponseEntity<>(new ResultObject<>(abnormalFormat), HttpStatus.OK);
    }

    /**
     * 根据条件获得异常详情格式列表
     */
    @ApiOperation("根据条件获得异常详情格式")
    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity<ResultObject> search() {
        Iterable<AbnormalFormatDTO> results = abnormalDetailFormatService.findAbnormalDetailFormats();
        return new ResponseEntity<>(new ResultObject<>(results), HttpStatus.OK);
    }

    @ApiOperation("绑定格式")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "long", paramType = "path"),
            @ApiImplicitParam(name = "pid", value = "ID", required = true, dataType = "long", paramType = "path")
    })
    @RequestMapping(value = "/{id}/pid/{pid}", method = RequestMethod.GET)
    public ResponseEntity<ResultObject> bind(@PathVariable("id") Long id,
                                             @PathVariable("pid") Long pid) {
        abnormalDetailFormatService.bind(id, pid);
        return new ResponseEntity<>(new ResultObject<>(null), HttpStatus.OK);
    }
}
